package NC45_实现二叉树先序_中序和后序遍历;

import java.util.*;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
}


public class Solution {
    /**
     *
     * @param root TreeNode类 the root of binary tree
     * @return int整型二维数组
     */
    ArrayList<Integer> listPre = new ArrayList<>();
    ArrayList<Integer> listIn = new ArrayList<>();
    ArrayList<Integer> listPost = new ArrayList<>();

    public int[][] threeOrders (TreeNode root) {
        // write code here
        if (root != null){
            preorder(root);
            inorder(root);
            postorder(root);
        }

        int[][] res = new int[3][listPre.size()];

        for (int i = 0; i < listPre.size(); i++){
            res[0][i] = listPre.get(i);
        }
        for (int i = 0; i < listIn.size(); i++){
            res[1][i] = listIn.get(i);
        }
        for (int i = 0; i < listPost.size(); i++){
            res[2][i] = listPost.get(i);
        }
        return res;
    }

    private void preorder(TreeNode root) {
        if (root == null){
            return;
        }

        listPre.add(root.val);
        preorder(root.left);
        preorder(root.right);
    }

    private void inorder(TreeNode root) {
        if (root == null){
            return;
        }

        inorder(root.left);
        listIn.add(root.val);
        inorder(root.right);
    }

    private void postorder(TreeNode root) {
        if (root == null){
            return;
        }

        postorder(root.left);
        postorder(root.right);
        listPost.add(root.val);
    }
}